package uutalk.netcloud.talk.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.app.Constants;
import uutalk.netcloud.talk.app.ui.ContentActivity;
import uutalk.netcloud.talk.model.bean.AppInfo;

/**
 * 类描述：
 * 作者：Pengmutian
 * 时间：2018/6/17
 */
public class AppInfoAdapter extends RecyclerView.Adapter<AppInfoAdapter.AppInfoViewHolder> {
    List<AppInfo> mlistAppInfo;
    private  Context context;
    public AppInfoAdapter(Context context, List<AppInfo> mlistAppInfo){
        this.context = context;
        this.mlistAppInfo = mlistAppInfo;
    }

    @Override
    public AppInfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_appinfo, parent,false);
        return new AppInfoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AppInfoViewHolder holder, int position) {
        final AppInfo appInfo = mlistAppInfo.get(position);
        holder.appIcon.setBackground(appInfo.getAppIcon());
        holder.tvName.setText(appInfo.getAppLabel());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if("1".equals(appInfo.getPkgName())){
                    //单聊界面
                    Intent intent = new Intent(context, ContentActivity.class);
                    intent.putExtra(Constants.KEY_FRAGMENT, Constants.SIGLE_FRAGMENT);
                    context.startActivity(intent);
                } else if("2".equals(appInfo.getPkgName())){
                    //群聊界面
                    Intent intent = new Intent(context, ContentActivity.class);
                    intent.putExtra(Constants.KEY_FRAGMENT, Constants.GROUP_FRAGMENT);
                    context.startActivity(intent);
                } else {
                    context.startActivity(appInfo.getIntent());
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return mlistAppInfo.size();
    }

    public static class AppInfoViewHolder extends RecyclerView.ViewHolder{
        TextView tvName;
        ImageView appIcon;
        public AppInfoViewHolder(View itemView) {
            super(itemView);
            appIcon = itemView.findViewById(R.id.ivApp);
            tvName = itemView.findViewById(R.id.tvApp);
        }
    }
}
